package cn.passlink.bletoothchat.mFragment;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.passlink.bletoothchat.Adapter.GroupsAdapter;
import cn.passlink.bletoothchat.Adapter.ScanGroupsAdapter;
import cn.passlink.bletoothchat.Entity.Groups;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.FriendsTableManager;
import cn.passlink.bletoothchat.SqlTables.GroupsTableManager;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.Tools.UserId.GroupIDGenerator;
import cn.passlink.bletoothchat.mActivity.RelationshipActivity;

public class AddGroupsScrollingFragment extends Fragment {

    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;
    private  Context mContext;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        // 初始化 databaseHelper
        databaseHelper = new MyDatabaseHelper(requireContext(), 2);
        return inflater.inflate(R.layout.fragment_add_groups_scrolling, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView searchButton=(ImageView) view.findViewById(R.id.search_groups_imageView);
        TextView crete_text=(TextView) view.findViewById(R.id.search_groups_editText);
        // 找到 ListView 对象
        ListView listView = view.findViewById(R.id.search_groups_ListView);

        mContext=getContext();

        // 创建 Adapter 对象
        List<Groups> GroupsList = new ArrayList<Groups>();
        Groups m=new Groups();
        m.setGroupname("Test");
        m.setMembers_amount("4567");
        m.setGroupid("45678");
        GroupsList.add(m);
        ScanGroupsAdapter adapter = new ScanGroupsAdapter(getContext(), GroupsList);
        // 设置适配器
        listView.setAdapter(adapter);

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(mContext, "搜索中==", Toast.LENGTH_SHORT).show();

            }
        });

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(database!=null){
            database.close();
        }
    }
    public boolean checkTableExists(SQLiteDatabase database, String tableName) {
        // 执行查询语句检查表是否存在
        String checkTableQuery = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
        Cursor cursor = database.rawQuery(checkTableQuery, null);

        // 检查结果集是否包含数据行
        boolean tableExists = cursor.moveToFirst();

        // 关闭游标
        cursor.close();

        // 返回结果
        return tableExists;
    }
}